<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="数据状态" clearable>
          <el-option v-for="dict in sys_normal_disable" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-upload
        action="http://mq.apisev.cn/py/data/upload" :show-file-list="false" with-credentials :on-success="afterUpload"
        >
          <el-button type="primary">新增</el-button>
        </el-upload>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate" v-hasPermi="['cms:category:update']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete" v-hasPermi="['cms:category:delete']">删除</el-button>
      </el-col> -->
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <!--数据列表-->
    <DataList @edit="handleEdit" @delete="handleDelete" :tableData="tableList" :loading="loading"></DataList>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
  </div>
</template>

<script setup name="Category">
import DataList from "../../components/DataList";
import { queryList, deleteOne, queryOne } from "../../api/data";
import userStore from "@/store/modules/user";
const isAdmin = ref(userStore().isAdmin);
const router = useRouter();
const { proxy } = getCurrentInstance();
const sys_normal_disable = ref([
  {
    value: 1,
    label: "启用",
  },
  {
    value: 0,
    label: "未启用",
  },
]);
const tableList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const total = ref(0);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10
  }
});

const { queryParams, form } = toRefs(data);

/** 查询分类列表 */
function getList() {
  loading.value = true;
  queryList(queryParams.value).then((response) => {
    tableList.value = response.content;
    total.value = response.totalElements;
    loading.value = false;
  });
}


/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 新增按钮操作 */
function afterUpload(){
  console.log('文件上传完成')
  handleQuery();
}
/** 提交按钮 */
function submitForm() {
  proxy.$refs["postRef"].validate((valid) => {
    if (valid) {
      if (form.value.id != undefined) {
        updateOne(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        createOne(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}
/** 删除按钮操作 */
function handleDelete(row) {
  // fix 删除 undefined 错误
  const deleteIds = row.id ? [row.id] : ids.value;
  console.log(deleteIds);
  proxy.$modal
    .confirm('是否确认删除分类编号为"' + deleteIds + '"的数据项？')
    .then(function () {
      let task = [];
      deleteIds.forEach(function (ele, index) {
        task.push(deleteOne(ele));
      });
      return new Promise(function (resolve, reject) {
        Promise.all(task).then((res) => {
          resolve(res);
        });
      });
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}
function handleEdit(row){
  router.push("/disk/data-detail/" + row.id + "");
}
// 修改状态
// function handleStatusChange(row) {
//   const data = row;
//   let text = row.status === 0 ? "停用" : "启用";
//   proxy.$modal
//     .confirm('确认要"' + text + '""' + row.name + "吗?")
//     .then(function () {
//       return updateOne(data);
//     })
//     .then(() => {
//       proxy.$modal.msgSuccess(text + "成功");
//     })
//     .catch(function () {
//       row.status = row.status === 0 ? 1 : 0;
//     });
// }
getList();
</script>
<style>

</style>
